﻿#include "error.h"

#include <QDebug>

#include <crtdbg.h>
#include <assert.h>

#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif

class errorPrivate {
public:
    static int          m_line;
    static bool         m_isError;
    static const char*  m_src;
};

int          errorPrivate::m_line = 0;
bool         errorPrivate::m_isError = 0;
const char*  errorPrivate::m_src = NULL;

error::error(int line, const char *file)
{
    d = new errorPrivate;
    d->m_line = line;
    d->m_src  = file;
    d->m_isError = false;
}

error &error::operator =(void *p)
{
    d->m_isError = false;

    if(NULL == p) {
        qDebug() << "error" << d->m_src << d->m_line;
        d->m_isError = true;
    }

    return *this;
}

bool error::operator =(QString errstr)
{
    if(!errstr.isNull() && d->m_isError){
        qDebug() << "error" << errstr.toStdString().c_str();
        return true;
    }

    return false;
}

error &error::operator =(int number)
{
    d->m_isError = false;

    if(number < 0) {
        qDebug() << "error" << d->m_src << d->m_line;
        d->m_isError = true;
    }

    return *this;
}

void error::setErrorStr(QString str)
{
    if(d->m_isError)
        qDebug() << "error" << str;
}

bool error::isFail()
{
    return d->m_isError;
}

error::~error()
{
	delete d;
}
